﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using Microsoft.Extensions.Logging;
using Sorting.App.Models;

using Sorting.App.Models.Enum;
using Modules.Units;
using HslCommunication.Profinet.Melsec;
using HslCommunication;
using System.IO.Ports;
using Furion.DependencyInjection;

namespace Modules.ActionRun
{


    /// <summary>
    /// 下发PLC动作执行帮助类
    /// </summary>
    public static class ActionRunHelper
    {
        public static ILog _log = LogManager.GetLogger(typeof(ActionRunHelper));

        public static bool ACon = false;

        public static bool BCon = false;


        /// <summary>
        /// 配置项目中A模组对应串口
        /// </summary>
        public static int StationNoA = AppsettingUnits.Appseeting.StationNoA;

        /// <summary>
        /// 配置项目中B模组对应串口
        /// </summary>
        public static int StationNoB = AppsettingUnits.Appseeting.StationNoB;

        public static MelsecFxSerial AFXCPU = new MelsecFxSerial();
        public static MelsecFxSerial BFXCPU = new MelsecFxSerial();

        #region 下发PLC指令

        /// <summary>
        /// 模块A PLC开启连接
        /// </summary>
        /// <returns></returns>
        public static bool PlcConnectA()
        {
            AFXCPU.SerialPortInni(sp =>
            {
                sp.PortName = "COM" + StationNoA.ToString();
                sp.BaudRate = 9600;
                sp.DataBits = 7;
                sp.StopBits = StopBits.One;
                sp.Parity = Parity.Even;
            });
            AFXCPU.Open();
            ACon = AFXCPU.IsOpen();
            return AFXCPU.IsOpen();
        }

        /// <summary>
        /// 模块B   PLC连接
        /// </summary>
        /// <returns></returns>
        public static bool PlcConnectB()
        {
            BFXCPU.SerialPortInni(sp =>
            {
                sp.PortName = "COM" + StationNoB.ToString();
                sp.BaudRate = 9600;
                sp.DataBits = 7;
                sp.StopBits = StopBits.One;
                sp.Parity = Parity.Even;
            });
            BFXCPU.Open();
            BCon = BFXCPU.IsOpen();
            return BFXCPU.IsOpen();
        }



        /// <summary>
        /// 选择PLC运行模式
        /// </summary>
        public static void SelectPlcModelType(EnumPlcModelType modelType)
        {

            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            AFXCPU.WriteAsync(PLCAddrModel.SelectPlcModelType, (short)modelType);
            BFXCPU.WriteAsync(PLCAddrModel.SelectPlcModelType, (short)modelType);

        }


        /// <summary>
        /// 选择PLC档位
        /// </summary>
        public static void SelectPlcGear(EnumPlcGear gear)
        {

            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            AFXCPU.WriteAsync(PLCAddrModel.SelectGear, (short)gear);
            BFXCPU.WriteAsync(PLCAddrModel.SelectGear, (short)gear);

        }



        /// <summary>
        /// 回原点
        /// </summary>
        /// <param name="group"></param>

        public static void RunBackOrigin(EnumStockGroup group)
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }

            if (group.Equals(EnumStockGroup.A))
            {
                //A_MFX.Write(PLCCMD.RunBackOrigin, 0, PLCCMD.RunBackOrigin.Length);
                AFXCPU.WriteAsync(PLCAddrModel.BackToOrigin, (short)1);
            }

            if (group.Equals(EnumStockGroup.B))
            {
                //B_MFX.Write(PLCCMD.RunBackOrigin, 0, PLCCMD.RunBackOrigin.Length);
                BFXCPU.WriteAsync(PLCAddrModel.BackToOrigin, (short)1);
            }
        }

        public static void SetStartCheck()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            AFXCPU.WriteAsync(PLCAddrModel.SetStartCheck, (short)1);
        }

        //public static short GetCheckState()
        //{
        //    if (!ACon)
        //    {
        //        PlcConnectA();
        //    }
        //    if (!BCon)
        //    {
        //        PlcConnectB();
        //    }
        //    short CheckState = 0;
        //    AFXCPU.WriteAsync(PLCAddrModel.CheckState, out CheckState);
        //    return CheckState;

        //}


        /// <summary>
        /// 写入储位
        /// </summary>
        /// <param name="group"></param>
        /// <param name="addr"></param>
        public static void RunToLoc(EnumStockGroup group, int addr)
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }

            if (group.Equals(EnumStockGroup.A))
            {
                var res = AFXCPU.WriteAsync(PLCAddrModel.WriteLoc, (short)addr);
            }

            if (group.Equals(EnumStockGroup.B))
            {
                var res = BFXCPU.WriteAsync(PLCAddrModel.WriteLoc, (short)addr);
            }
        }




        #endregion

        #region  调试专用公开方法


        /// <summary>
        /// X轴回原点
        /// </summary>
        /// <param name="group"></param>
        public static void RunXToOrgin(EnumStockGroup group)
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            if (group.Equals(EnumStockGroup.A))
            {
                AFXCPU.WriteAsync(PLCAddrModel.RunOriginX, (short)1);
            }
            else if (group.Equals(EnumStockGroup.B))
            {
                BFXCPU.WriteAsync(PLCAddrModel.RunOriginX, (short)1);
            }

        }


        /// <summary>
        /// Z轴回原点
        /// </summary>
        /// <param name="group"></param>
        public static void RunZToOrgin(EnumStockGroup group)
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            if (group.Equals(EnumStockGroup.A))
            {
                AFXCPU.WriteAsync(PLCAddrModel.RunOriginZ, (short)1);
            }
            else if (group.Equals(EnumStockGroup.B))
            {
                BFXCPU.WriteAsync(PLCAddrModel.RunOriginZ, (short)1);
            }

        }

        /// <summary>
        /// 输入皮带运行  --调试使用
        /// </summary>
        /// <param name="group"></param>
        public static void RunInLineDebug(EnumStockGroup group)
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            if (group.Equals(EnumStockGroup.A))
            {
                AFXCPU.WriteAsync(PLCAddrModel.RunInLine, (short)1);
            }
            else if (group.Equals(EnumStockGroup.B))
            {
                BFXCPU.WriteAsync(PLCAddrModel.RunInLine, (short)1);
            }

        }

        /// <summary>
        /// 转向皮带运行  --调试使用
        /// </summary>
        /// <param name="group"></param>
        public static void RunReturnLineDebug(EnumStockGroup group)
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            if (group.Equals(EnumStockGroup.A))
            {
                AFXCPU.WriteAsync(PLCAddrModel.RunReturnLine, (short)1);
            }
            else if (group.Equals(EnumStockGroup.B))
            {
                BFXCPU.WriteAsync(PLCAddrModel.RunReturnLine, (short)1);
            }

        }

        /// <summary>
        /// 托盘皮带正反转
        /// </summary>
        /// <param name="group">模组</param>
        /// <param name="IsForward">正反转 1：正转，2：反转</param>
        public static void RunTray(EnumStockGroup group, int IsForward)
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            if (group.Equals(EnumStockGroup.A))
            {
                AFXCPU.WriteAsync(PLCAddrModel.RunTray, (short)IsForward);
            }
            else if (group.Equals(EnumStockGroup.B))
            {
                BFXCPU.WriteAsync(PLCAddrModel.RunTray, (short)IsForward);
            }

        }

        /// <summary>
        /// 设置X轴速度
        /// </summary>
        /// <param name="group"></param>
        /// <param name="Speed"></param>
        public static void SetXSpeed(EnumStockGroup group, int Speed)
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            if (group.Equals(EnumStockGroup.A))
            {
                AFXCPU.WriteAsync(PLCAddrModel.SetXSpeed, (short)Speed);
            }
            else if (group.Equals(EnumStockGroup.B))
            {
                BFXCPU.WriteAsync(PLCAddrModel.SetXSpeed, (short)Speed);
            }

        }


        /// <summary>
        /// 设置Z轴速度
        /// </summary>
        /// <param name="group"></param>
        /// <param name="Speed"></param>
        public static void SetZSpeed(EnumStockGroup group, int Speed)
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            if (group.Equals(EnumStockGroup.A))
            {
                AFXCPU.WriteAsync(PLCAddrModel.SetZSpeed, (short)Speed);
            }
            else if (group.Equals(EnumStockGroup.B))
            {
                BFXCPU.WriteAsync(PLCAddrModel.SetZSpeed, (short)Speed);
            }

        }


        /// <summary>
        /// 单模组整体回原点
        /// </summary>
        /// <param name="group"> 模组</param>
        public static void RunToOrgin(EnumStockGroup group)
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            if (group.Equals(EnumStockGroup.A))
            {

                AFXCPU.WriteAsync(PLCAddrModel.BackToOrigin, (short)1);
            }
            else if (group.Equals(EnumStockGroup.B))
            {

                BFXCPU.WriteAsync(PLCAddrModel.BackToOrigin, (short)1);

            }

        }

        /// <summary>
        /// 运行到指定储位    调试使用
        /// </summary>
        /// <param name="group">模组</param>
        ///  <param name="LocAddr">储位地址</param>
        public static void DebugRunToLoc(EnumStockGroup group, int LocAddr)
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            if (group.Equals(EnumStockGroup.A))
            {
                AFXCPU.WriteAsync(PLCAddrModel.WriteLoc, (short)LocAddr);
            }
            else if (group.Equals(EnumStockGroup.B))
            {
                BFXCPU.WriteAsync(PLCAddrModel.WriteLoc, (short)LocAddr);
            }

        }
        #endregion


        #region  设备监控方法
        public static void StartMonitor()
        {
            ReadXPosLimit();
            ReadXNegLimit();

            ReadZPosLimit();
            ReadZNegLimit();

            ReadXOrgin();
            ReadZOrgin();
            //ReadXBackOrgin();
            ReadAllToOrginA();
            ReadAllToOrginB();
            //ReadZBackOrgin();
            ReadInlineStartPoint();
            ReadInlineEndPoint();
            ReadReturnLinePoint();
            ReadTrayBeginPoint();
            ReadTrayMiddlePoint();
            ReadTrayFinishPoint();
            ReadTowTrackSafe();

            ReadXMotor();
            ReadZMotor();
            //ReadXOrgin();
            //ReadZOrgin();


        }

        /// <summary>
        /// A模组回原点
        /// </summary>
        /// <returns></returns>
        public static short ReadAllToOrginA()
        {
            if (!ACon)
            {
                PlcConnectA();
            }

            var OrginA = AFXCPU.ReadInt16Async(PLCAddrModel.OriginStates, 1);
            AppsettingUnits.SignalModel.AllToOrginA = OrginA.Result.Content[0];
            return OrginA.Result.Content[0];
        }

        /// <summary>
        /// B模组回原点
        /// </summary>
        /// <returns></returns>
        public static short ReadAllToOrginB()
        {


            var OrginA = BFXCPU.ReadInt16Async(PLCAddrModel.OriginStates, 1);
            AppsettingUnits.SignalModel.AllToOrginB = OrginA.Result.Content[0];
            return OrginA.Result.Content[0];
        }


        static short OldStates = 1;
        /// <summary>
        /// 获取入口点状态
        /// </summary>
        public static async void GetInLinePoint()
        {
            if (!ACon)
            {
                PlcConnectA();
            }

            //short NewStates = 0;
            AppsettingUnits.SignalModel.InLinePoint = false;
           var NewStates= await AFXCPU.ReadInt16Async(PLCAddrModel.InLineStartPoint);

            //_log.Debug("入队信号：" + NewStates);

            if (OldStates != NewStates.Content && NewStates.Equals(1))
            {
                AppsettingUnits.SignalModel.InLinePoint = true;
                OldStates = NewStates.Content;
            }
            OldStates = NewStates.Content;
        }

        static int EndDrugOldStates = 0;
        /// <summary>
        /// 获取是否是最后一袋
        /// </summary>
        public static async void GetEndDrugPoint()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            // short NewStates = 0;
            AppsettingUnits.SignalModel.EndDrugPoint = false;
            var NewStates =await AFXCPU.ReadInt16Async(PLCAddrModel.IfEndDrug);
            if (EndDrugOldStates != NewStates.Content && NewStates.Equals(1))
            {
                EndDrugOldStates = NewStates.Content;
                AppsettingUnits.SignalModel.EndDrugPoint = true;
            }
            EndDrugOldStates = NewStates.Content;


        }

        /// <summary>
        /// 获取机械手状态
        /// </summary>;
        //public static void GetRobotIsBusy()
        //{
        //    if (!ACon)
        //    {
        //        PlcConnectA();
        //    }
        //    short NewStates = 0;
        //    short BNewStates = 0;
        //    //获取A模组状态
        //    AFXCPU.ReadInt16Async(PLCAddrModel.ReadIsSucess, out NewStates);

        //    //写入缓存
        //    if (!BCon)
        //    {
        //        PlcConnectB();
        //    }
        //    //获取B模组状态
        //    BFXCPU.ReadInt16Async(PLCAddrModel.ReadIsSucess, out BNewStates);
        //}



        /// <summary>
        /// 获取回原点状态
        /// </summary>
        public static short GetbackOrgState()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            // short NewStates = 0;
            var NewStates = AFXCPU.ReadInt16Async(PLCAddrModel.OriginStates);
            AppsettingUnits.SignalModel.BackOrgState = NewStates.Result.Content;
            return NewStates.Result.Content;
        }





        //============================== 监控PLC地址 ==============================

        /// <summary>
        ///  读取X轴正极限  AB双模组
        /// </summary>
        /// <param name="XPosLimitA">A模组状态</param>
        /// <param name="XPosLimitB">B模组状态</param>
        public static async void ReadXPosLimit()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //short PosLimitA;
            //short PosLimitB;

            var PosLimitA =await AFXCPU.ReadInt16Async(PLCAddrModel.XPositiveLimit);
            var PosLimitB =await BFXCPU.ReadInt16Async(PLCAddrModel.XPositiveLimit);
            AppsettingUnits.SignalModel.XPosLimitA = PosLimitA.Content;
            AppsettingUnits.SignalModel.XPosLimitB = PosLimitB.Content;

        }

        /// <summary>
        /// 读取X轴反极限  AB双模组
        /// </summary>
        /// <param name="ZPosLimitA">A模组状态</param>
        /// <param name="ZPosLimitB">B模组状态</param>
        public static void ReadXNegLimit()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //short NegLimitA;
            //short NegLimitB;

            var NegLimitA = AFXCPU.ReadInt16Async(PLCAddrModel.XNegativeLimit);
            var NegLimitB = BFXCPU.ReadInt16Async(PLCAddrModel.XNegativeLimit);
            AppsettingUnits.SignalModel.XNegLimitA = NegLimitA.Result.Content;
            AppsettingUnits.SignalModel.XNegLimitB = NegLimitB.Result.Content;
        }



        /// <summary>
        ///  读取Z轴正极限  AB双模组
        /// </summary>
        /// <param name="XPosLimitA">A模组状态</param>
        /// <param name="XPosLimitB">B模组状态</param>
        public static async void ReadZPosLimit()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //short PosLimitA;
            //short PosLimitB;

            var PosLimitA =await AFXCPU.ReadInt16Async(PLCAddrModel.ZPositiveLimit);
            var PosLimitB =await BFXCPU.ReadInt16Async(PLCAddrModel.ZPositiveLimit);
            AppsettingUnits.SignalModel.ZPosLimitA = PosLimitA.Content;
            AppsettingUnits.SignalModel.ZPosLimitB = PosLimitB.Content;
        }

        /// <summary>
        /// 读取Z轴反极限  AB双模组
        /// </summary>
        /// <param name="ZPosLimitA">A模组状态</param>
        /// <param name="ZPosLimitB">B模组状态</param>
        public static async void ReadZNegLimit()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //short NegLimitA;
            //short NegLimitB;

            var NegLimitA =await AFXCPU.ReadInt16Async(PLCAddrModel.ZNegativeLimit);
            var NegLimitB =await BFXCPU.ReadInt16Async(PLCAddrModel.ZNegativeLimit);
            AppsettingUnits.SignalModel.ZNegLimitA = NegLimitA.Content;
            AppsettingUnits.SignalModel.ZNegLimitB = NegLimitB.Content;
        }


        /// <summary>
        /// 读取X轴原点
        /// </summary>
        /// <param name="XOrginA">A模组状态</param>
        /// <param name="XOrginB">B模组状态</param>
        public static void ReadXOrgin()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //short XOrginA;
            //short XOrginB;

            var XOrginA = AFXCPU.ReadInt16Async(PLCAddrModel.XOrigin);
            var XOrginB = BFXCPU.ReadInt16Async(PLCAddrModel.XOrigin);
            AppsettingUnits.SignalModel.XOrginA = XOrginA.Result.Content;
            AppsettingUnits.SignalModel.XOrginB = XOrginB.Result.Content;
        }


        /// <summary>
        /// 读取Z轴原点
        /// </summary>
        /// <param name="ZOrginA">A模组状态</param>
        /// <param name="ZOrginB">B模组状态</param>
        public static void ReadZOrgin()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //short ZOrginA;
            //short ZOrginB;
            var ZOrginA = AFXCPU.ReadInt16Async(PLCAddrModel.ZOrigin);
            var ZOrginB = BFXCPU.ReadInt16Async(PLCAddrModel.ZOrigin);
            AppsettingUnits.SignalModel.ZOrginA = ZOrginA.Result.Content;
            AppsettingUnits.SignalModel.ZOrginB = ZOrginB.Result.Content;
        }


        /// <summary>
        /// 两轴距离过近传感器  0 距离过近   1  正在运行
        /// </summary>
        public static void ReadTowTrackSafe()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //  short STowTrackSafe;
            var STowTrackSafe = AFXCPU.ReadInt16Async(PLCAddrModel.RailSafety);
            AppsettingUnits.SignalModel.STowTrackSafe = STowTrackSafe.Result.Content;

        }

        /// <summary>
        /// 输入皮带开始位置
        /// </summary>
        public static void ReadInlineStartPoint()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }

            var SInlineStartPoint = AFXCPU.ReadInt16Async(PLCAddrModel.InLineBeginPoint);
            AppsettingUnits.SignalModel.SInlineStartPoint = SInlineStartPoint.Result.Content;

        }

        /// <summary>
        /// 输入皮带结束位置
        /// </summary>
        public static void ReadInlineEndPoint()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            // short SInlineEndPoint;
            var SInlineEndPoint = AFXCPU.ReadInt16Async(PLCAddrModel.InLineFinishPoint);
            AppsettingUnits.SignalModel.SInlineEndPoint = SInlineEndPoint.Result.Content;
        }


        /// <summary>
        /// 转向皮带传感器
        /// </summary>
        /// <param name="ReturnLinePoint"></param>
        public static void ReadReturnLinePoint()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }

            var SReturnLinePoint = AFXCPU.ReadInt16Async(PLCAddrModel.TrunLinePoint);
            AppsettingUnits.SignalModel.SReturnLinePoint = SReturnLinePoint.Result.Content;

        }



        /// <summary>
        /// 托盘开始位置
        /// </summary>
        /// <param name="TrayBeginPoint"></param>
        public static void ReadTrayBeginPoint()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //short STrayBeginPointA;
            //short STrayBeginPointB;
            var STrayBeginPointA = AFXCPU.ReadInt16Async(PLCAddrModel.TrayBeginPoint);
            var STrayBeginPointB = BFXCPU.ReadInt16Async(PLCAddrModel.TrayBeginPoint);
            AppsettingUnits.SignalModel.STrayBeginPointA = STrayBeginPointA.Result.Content;
            AppsettingUnits.SignalModel.STrayBeginPointB = STrayBeginPointB.Result.Content;
        }

        /// <summary>
        /// 托盘中间位置
        /// </summary>
        /// <param name="TrayMiddlePoint"></param>
        public static void ReadTrayMiddlePoint()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //short STrayMiddlePointA;
            //short STrayMiddlePointB;
            var STrayMiddlePointA = AFXCPU.ReadInt16Async(PLCAddrModel.TrayMiddlePoint);
            var STrayMiddlePointB = BFXCPU.ReadInt16Async(PLCAddrModel.TrayMiddlePoint);
            AppsettingUnits.SignalModel.STrayMiddlePointA = STrayMiddlePointA.Result.Content;
            AppsettingUnits.SignalModel.STrayMiddlePointB = STrayMiddlePointB.Result.Content;
        }


        /// <summary>
        /// 托盘结束位置
        /// </summary>
        /// <param name="TrayMiddlePoint"></param>
        public static void ReadTrayFinishPoint()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //short STrayFinishPointA;
            //short STrayFinishPointB;
            var STrayFinishPointA = AFXCPU.ReadInt16Async(PLCAddrModel.TrayFinishPoint);
            var STrayFinishPointB = BFXCPU.ReadInt16Async(PLCAddrModel.TrayFinishPoint);
            AppsettingUnits.SignalModel.STrayFinishPointA = STrayFinishPointA.Result.Content;
            AppsettingUnits.SignalModel.STrayFinishPointB = STrayFinishPointB.Result.Content;
        }


        /// <summary>
        /// X电机使能
        /// </summary>
        public static void ReadXMotor()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //short SXMotorA;
            //short SXMotorB;
            var SXMotorA = AFXCPU.ReadInt16Async(PLCAddrModel.XMotorMake);
            var SXMotorB = BFXCPU.ReadInt16Async(PLCAddrModel.XMotorMake);
            AppsettingUnits.SignalModel.SXMotorA = SXMotorA.Result.Content;
            AppsettingUnits.SignalModel.SXMotorB = SXMotorB.Result.Content;
        }



        /// <summary>
        /// Z电机使能
        /// </summary>
        public static void ReadZMotor()
        {
            if (!ACon)
            {
                PlcConnectA();
            }
            if (!BCon)
            {
                PlcConnectB();
            }
            //short SZMotorA;
            //short SZMotorB;
            var SZMotorA = AFXCPU.ReadInt16Async(PLCAddrModel.ZMotorMake);
            var SZMotorB = BFXCPU.ReadInt16Async(PLCAddrModel.ZMotorMake);
            AppsettingUnits.SignalModel.SZMotorA = SZMotorA.Result.Content;
            AppsettingUnits.SignalModel.SZMotorB = SZMotorB.Result.Content;
        }




        ///// <summary>
        ///// X回原点状态
        ///// </summary>
        //public  static void ReadXBackOrgin()
        //{
        //    if (!ACon)
        //    {
        //        PlcConnectA();
        //    }
        //    if (!BCon)
        //    {
        //        PlcConnectB();
        //    }
        //    short SXOrginA;
        //    short SXOrginB;
        //    AFXCPU.GetDevice2(PLCAddrModel.XBackOrgin, out SXOrginA);
        //    BFXCPU.GetDevice2(PLCAddrModel.XBackOrgin, out SXOrginB);
        //    Appsetting.SignalModel.SXOrginA = SXOrginA;
        //    Appsetting.SignalModel.SXOrginB = SXOrginB;
        //}



        ///// <summary>
        ///// Z回原点状态
        ///// </summary>
        //public  static void ReadZBackOrgin()
        //{
        //    if (!ACon)
        //    {
        //        PlcConnectA();
        //    }
        //    if (!BCon)
        //    {
        //        PlcConnectB();
        //    }
        //    short SZOrginA;
        //    short SZOrginB;
        //    AFXCPU.GetDevice2(PLCAddrModel.ZBackOrgin, out SZOrginA);
        //    BFXCPU.GetDevice2(PLCAddrModel.ZBackOrgin, out SZOrginB);
        //    Appsetting.SignalModel.SZOrginA = SZOrginA;
        //    Appsetting.SignalModel.SZOrginB = SZOrginB;
        //}




        #endregion


        #region 看门狗 PLC存活检测

        public static short GetPlcLifeCmd()
        {
            try
            {
                //short PLCLife = 0;
                var PLCLife = AFXCPU.ReadInt16Async(PLCAddrModel.PLCLife);
                //_log.Debug(PLCLife);
                return PLCLife.Result.Content;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }


        }



        #endregion


        #region 看门狗 PLC存活检测

        public static void GetSoftWareLifeCmd()
        {
            AFXCPU.WriteAsync(PLCAddrModel.SoftWareLife, (short)1);
            //_log.Debug("写入软件存活");
        }



        #endregion

    }
}
